<!--
 * @Author: cqg
 * @Date: 2020-12-10 09:47:34
 * @LastEditTime: 2021-04-25 15:36:57
 * @Description: file content
 * @LastEditors: xjt
-->

<template>
  <div :class="{previewPage: type === 'preview'}">
    <el-dialog :title="title" class="basic-from" :visible.sync="formVisible" width="1200px" @close="close">
      <el-form :model="form" class="form-wrap" ref="form" :rules="rules">
        <p class="title full">工程信息</p>
        <el-form-item label="工程名称" prop="projectName">
          <el-input
            v-halfCharCode
            v-model="form.projectName"
            :disabled="type=='preview'"
            suffix-icon="el-icon-search"
            @focus="$refs.chooseDialog.show()"
          />
        </el-form-item>
        <el-form-item label="工程地址" prop="projectAddress">
          <el-input v-halfCharCode disabled v-model="form.projectAddress" />
        </el-form-item>
        <el-form-item label="工程类型" prop="projectType">
          <el-select clearable disabled v-model="form.projectType" placeholder="-=请选择=-" filterable>
            <el-option v-for="item in epcProjectTypes" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <p class="title full">资产移交</p>
        <el-form-item label="文号" prop="transferDocument">
          <el-input v-halfCharCode :disabled="type=='preview'" v-model="form.transferDocument" maxlength="20" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="金额(元)" prop="transferAmount">
          <el-input v-if="type!=='preview'" placeholder="请输入" v-model="form.transferAmount" v-only-number="{min:0,precision:2}" />
          <el-input v-else :value="form.transferAmount | thousandMoney" />
        </el-form-item>
        <el-form-item label="移交日期" prop="transferDate">
          <el-date-picker :disabled="type=='preview'" v-model="form.transferDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" />
        </el-form-item>
        <el-form-item label="意见" prop="remarks">
          <el-input v-model="form.remarks" maxlength="100" placeholder="请输入" />
        </el-form-item>
        <p class="title full">附件</p>
      </el-form>
      <v-upload
        ref="upload"
        v-if="formVisible"
        :busId="busId"
        modelType="transferAssets"
        @onSuccess="onSuccess"
        v-model="uploadFileList"
        :isView="['preview'].includes(type)"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="$bus.$emit('operationLog', form.projectId)" v-if="type === 'preview'">操作日志</el-button>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" v-throttle="[save]" v-if="['add','edit'].includes(type)">保存</el-button>
      </div>
    </el-dialog>
    <choose-dialog ref="chooseDialog" v-bind="chooseDialogAttrs" @chooseData="handleChoose" />
  </div>
</template>
<script>
import {
  updateOrSave,
  validateCode
} from "./api.js";
import { getDict } from "@/utils";
export default {
  name: "transferAssets",
  inject: ["initList"],
  data () {
    return {
      //上传id
      busId: "",
      // 上传列表
      uploadFileList: [],
      //弹框title
      title: "",
      //操作类型
      type: "",
      //验收弹窗开关
      formVisible: false,
      // 表单数据
      form: {
        projectId: "",//工程id
        projectTransferId: "",//当前模块主键id
        projectFinalId: "",//上级流程id
        projectName: "",
        projectType: "",
        projectAddress: "",
        transferDocument: "", //文号
        transferDate: "", //决算日期
        transferAmount: "", //金额
        remarks: ""
      },
      //表单验证规则
      rules: {
        projectName: [
          {
            required: true,
            message: "请填选完整工程信息",
            trigger: "change"
          }
        ],
        transferDocument: [
          {
            required: true,
            message: "请输入文号",
            trigger: "change"
          },
          { validator: this.validateSettle, trigger: "blur" }
        ],
        transferAmount: [
          {
            required: true,
            message: "请输入金额",
            trigger: "change"
          },
        ],
        transferDate: [
          {
            required: true,
            message: "请输入移交日期",
            trigger: "change"
          },
        ],
      }
    };
  },
  computed: {
    // 工程类型
    epcProjectTypes () {
      return getDict("epc_project_type");
    },
    //弹框勾选列表配置
    chooseDialogAttrs () {
      return {
        title: "选择工程",
        placeholder: "请输入工程名称",
        url: "/api/epcprojecttransfer/selectProject",
        primaryKey: "projectId",
        width: "1000px",
        searchKey: "projectName",
        showIndex: true,
        showData: [
          {
            prop: "projectName",
            label: "工程名称",
            align: "center"
          },
          {
            prop: "projectAddress",
            label: "工程地址",
            align: "center"
          }
        ]
      }
    }
  },
  methods: {
    //   结算 唯一校验
    async validateSettle (rule, value, callback) {
      let rps = await validateCode({
        transferDocument: value,
        projectTransferId: this.form.projectTransferId
      });
      if (rps.data.data == "1" && rps.data.code == '0') callback(new Error("该文号已存在"));
      callback();
    },
    // 关闭弹窗清空发表单
    close () {
      this.uploadFileList.length = 0;
      for (var i in this.form) {
        this.form[i] = Object.prototype.toString.call(this.form[i]) == "[object Array]" ? [] : "";
      }
      this.formVisible = false;
      this.$refs.form.clearValidate();
    },
    // 监听弹窗选中
    handleChoose (data) {
      this.form = { ...this.form, ...data };
      this.form.remarks = ''
    },
    //显示弹窗
    show (type, data = {}) {
      // 判断类型渲染表单标题
      switch (type) {
        case "add":
          this.title = "新增";
          break;
        case "edit":
          this.title = "编辑";
          break;
        case "preview":
          this.title = "查看";
          break;
      }
      this.type = type;
      this.formVisible = true;
      this.form = { ...this.form, ...data };
      if (data) this.busId = this.form.projectTransferId;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      })
    },
    // 保存
    async save () {
      if (["edit", "add"].includes(this.type)) {
        this.$refs.form.validate(async valid => {
          valid && this.$refs.upload.submit();
        });
      }
    },
    // 上传成功
    async onSuccess (response, file, fileList) {
      let params =
        this.type == "add"
          ? { ...this.form, projectTransferId: response.data }
          : JSON.parse(JSON.stringify(this.form));
      const rps = await updateOrSave(this.type, params);
      if (rps.data.code == 0) {
        this.$message.success(`提交成功!`);
        this.formVisible = false;
        this.initList({ projectName: "", current: 1 });
      } else this.$message.error(rps.data.msg);
    }
  }
};
</script>
<style lang="scss" scoped>
.title {
  border-bottom: 1px solid rgba(97, 106, 120, 0.07);
  height: 50px;
  line-height: 50px;
  font-weight: 700;
  font-size: 16px;
}
/deep/.el-dialog__body {
  padding: 16px 32px;
}
</style>
